package com.ziytek.web.citizen.service;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.ziytek.web.citizen.constant.NumConstant;
import com.ziytek.web.citizen.mapper.BasicStepSettingsMapper;
import com.ziytek.web.citizen.mapper.SettingDetailMapper;
import com.ziytek.web.citizen.model.bean.AddSettings;
import com.ziytek.web.citizen.pojo.basic.SettingDetail;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;


/**
 * 执行工步设置
 *
 * @author fenghx
 */
@Slf4j
@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class SettingService {

    private final Gson gson;
    private final SettingDetailMapper settingDetailMapper;
    private final BasicStepSettingsMapper basicStepSettingsMapper;

    /***
     * 保存正常工步设置
     *
     * @param setting 工步设置
     * @param stepId 工步方案id
     * @param order  工步执行序列号
     */
    public void settingDetailSave(AddSettings setting, long stepId, int order) {
        if (setting == null) {
            return;
        }

        var sd = new SettingDetail();
        sd.setStepId(stepId);
        sd.setSettingOrder(order);
        sd.setCutOff(setting.getCutOff());
        sd.setInterruptOrEndStepId(stepId);
        sd.setSetType(setting.getSetType());
        sd.setCurrent(setting.getCurrent());
        sd.setVoltage(setting.getVoltage());
        sd.setIsDelete(NumConstant.INT_ZERO);
        sd.setCutOffType(setting.getCutOffType());
        sd.setUpdateTime(System.currentTimeMillis());
        sd.setCreateTime(System.currentTimeMillis());
        sd.setExpectedTime(setting.getExpectedTime());

        var seq = settingDetailMapper.queryMaxId();
        sd.setId(seq == null ? NumConstant.LONG_ONE : seq);
        settingDetailMapper.insertData(sd);
    }


    /***
     * 保存终止或者结束工步设置
     *
     * @param stepId  工步方案ID -1-终止 -100-结束
     * @param interruptOrEndStepId  关联的终止或者结束工步ID
     * @param order  工步执行序列号
     */
    public void endSettingDetailSave(Long stepId, int order, Long interruptOrEndStepId) {
        var basic = basicStepSettingsMapper.findFirstById(stepId);
        if (basic == null) {
            return;
        }

        List<AddSettings> settingLists = gson.fromJson(basic.getStepSettings(), new TypeToken<List<AddSettings>>() {
        }.getType());

        if (CollectionUtils.isEmpty(settingLists)) {
            return;
        }

        var setting = settingLists.getFirst();
        var s = new SettingDetail();
        s.setSettingOrder(order);
        s.setStepId(basic.getId());
        s.setCutOff(setting.getCutOff());
        s.setSetType(setting.getSetType());
        s.setCurrent(setting.getCurrent());
        s.setVoltage(setting.getVoltage());
        s.setIsDelete(NumConstant.INT_ZERO);
        s.setCutOffType(setting.getCutOffType());
        s.setUpdateTime(System.currentTimeMillis());
        s.setCreateTime(System.currentTimeMillis());
        s.setInterruptOrEndStepId(interruptOrEndStepId);
        s.setExpectedTime(setting.getExpectedTime());

        var seq = settingDetailMapper.queryMaxId();
        s.setId(seq == null ? NumConstant.LONG_ONE : seq);
        settingDetailMapper.insertData(s);
    }
}
